package com.xidian.zhiancloud.controller.systemset.usermanage;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.controller.systemset.UserController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TrainingRiskStatusList;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import com.xidian.zhiancloud.util.tools.ExcelUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 组织机构
 *
 *
 */
@RestController
@RequestMapping("/org")
public class OrgController extends BaseController {

    @Autowired
    OrganizationService organizationService;

    @Autowired
    UserService userService;

    @Autowired
    HttpServletRequest request;

    /**
     * 添加组织机构部门
     * @param org 组织机构部门信息
     * @return 返回ResultDTO 对象
     */
    @RequestMapping("/addOrgMessage")
    public ResultDTO addOrgMessage(Organization org){
        if (org == null){
            return ResultDTO.errorOf(UserResponseEnums.ORG_MESSAGE_NOT_NULL);
        }else{
            Long companyId = this.getSessionUserCompanyId();
//            String name = org.getName();
//            Integer orglevelid = org.getOrglevelid();
//            Organization organization = organizationService
//                .selectByNameAndOrglevelid(companyId,name,orglevelid);
//            if(organization != null){
//                return ResultDTO.errorOf(UserResponseEnums.ORG_ALREADY_EXISTS);
//            }else{
//                if ("1".equals(org.getIsafeorg())){
//                    org.setIsafeorg(1);
//                }else if ("0".equals(org.getIsafeorg())){
//                    org.setIsafeorg(0);
//                }
//                org.setCompanyId(companyId);
//                organizationService.addOrgMessage(org);
//            }
            if ("1".equals(org.getIsafeorg())){
                org.setIsafeorg(1);
            }else if ("0".equals(org.getIsafeorg())){
                org.setIsafeorg(0);
            }
            org.setCompanyId(companyId);
            organizationService.addOrgMessage(org);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除组织机构管理
     * @param id 组织机构管理Id
     * @return 返回ResultDTO 对象
     */
    @RequestMapping("/deleteOrgById")
    public ResultDTO deleteOrgById(@RequestParam("id") String id){
        organizationService.deleteOrg(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 编辑组织机构部门
     * @param org 组织机构部门信息
     * @param id  组织机构部门Id
     * @return 返回ResultDTO 对象
     */
    @RequestMapping("/updateOrgMessage")
    public ResultDTO updateOrgMessage
    (@RequestParam("id") String id,Organization org){
        if (org == null){
            return ResultDTO.errorOf(UserResponseEnums.ORG_MESSAGE_NOT_NULL);
        } else {
            organizationService.updateOrgMessage(id,org);
//            Long companyId = this.getSessionUserCompanyId();
//            String name = org.getName();
//            Integer orglevelid = org.getOrglevelid();
//            Organization organization = organizationService
//               .selectByNameAndOrglevelid(companyId,name,orglevelid);
//            if(organization != null && !id.equals(organization.getId())) {
//                return ResultDTO.errorOf(UserResponseEnums.ORG_ALREADY_EXISTS);
//            } else {
//                organizationService.updateOrgMessage(id,org);
//            }
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 组织机构部门列表导出
     */
    @GetMapping("/exportOrgList")
    public void exportOrgList(HttpServletResponse response) throws IOException {
        Long companyId=this.getSessionUserCompanyId();
        Page<Organization> pagedata = userService
            .getexportOrgList(companyId);
        List<Organization> list = pagedata.getResult();
        ExcelUtil2<Organization> util = new
            ExcelUtil2<Organization>(Organization.class);
        util.exportExcel(response, list, "组织机构部门");
    }


}
